package com.xielaoban.cqueshop.Admin.Controller;

import com.github.pagehelper.PageInfo;
import com.xielaoban.cqueshop.Common.Result;
import com.xielaoban.cqueshop.Entity.Order.Order;
import com.xielaoban.cqueshop.Service.OrderService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author 蟹老板
 * @Date 2021-4-15 20:50
 * @Version 1.0
 * @Package com.xielaoban.cqueshop.Admin.Controller
 * @Description
 */
@CrossOrigin
@RestController
@RequestMapping("/admin/order")
public class AdminOrderController {
    private static final Log log = LogFactory.getLog(AdminOrderController.class);

    @Autowired
    private OrderService orderService;

    @GetMapping("/get/{id}")
    public Result get(@PathVariable String id) {
        try {
            log.info("获取全部订单！");
            Order order = orderService.get(id);
            return Result.Success(order);
        } catch (Exception e) {
            log.error("获取全部订单出错了！", e);
            return Result.Error();
        }
    }

    @GetMapping("/getAll")
    public Result getAll(Integer pageSize, Integer currentPage) {
        try {
            log.info("获取全部订单！");
            PageInfo<Order> orderPageInfo = orderService.getAll(pageSize, currentPage);
            return Result.Success(orderPageInfo);
        } catch (Exception e) {
            log.error("获取全部订单出错了！", e);
            return Result.Error();
        }
    }

    @PostMapping("/update")
    public Result update(@RequestBody Order order) {
        try {
            log.info("更新订单！");
            int result = orderService.update(order);
            return Result.Success(result);
        } catch (Exception e) {
            log.error("更新订单出错了！", e);
            return Result.Error();
        }
    }

    @DeleteMapping("/del/{id}")
    public Result del(@PathVariable String id) {
        try {
            log.info("删除订单！" + id);
            int result = orderService.del(id);
            return Result.Success(result);
        } catch (Exception e) {
            log.error("删除订单出错了！", e);
            return Result.Error();
        }
    }
}
